<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */

/**
 * Replace cached inserts with the actual results
 *
 * @param string $results
 * @return string
 */
function smarty_insert_pp_pdprice($params, &$smarty) {
	if( isset($params['value']) ) {
		$value = $params['value'];
		$type = $params['type'];
		$ts = explode(':', $type);
		$format = $ts['1'];
		$dsct = $ts['2'];
		
		if( CONST_PRICE_MODE_TYPE == CONST_PRICE_MODE_UCT ) {
			global $ppmuctObj;
			
			$product = '';
			if( is_string($value) && strstr($value, '|') !== false ) {
				$ss = explode('|', $value);
				$product = array(
						'products_price' => $ss['0'],
						'products_price1' => $ss['1'],
						'products_price2' => $ss['2'],
						'products_price3' => $ss['3'],
						'products_price4' => $ss['4'],
						'products_price5' => $ss['5'],
						'categories_id' => $ss['6'],
							);
			} elseif( is_array($value) ) {
				$product = $value;
			}
			if( !is_object($ppmuctObj) ) {
				include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'product.price.model.uct.class.php');
				$ppmuctObj = new ProductPriceModelUCTBase();
			}
			if( is_array($product) ) {//product data
				$ppmuctObj->setProduct($product);
				
				$value = $ppmuctObj->getProductPrice();
			}
		} else {
			if( $_SESSION['suser']->userid > 0 && $_SESSION['suser']->discount > 0 ) {
				$value = floatval($value) * (1-$_SESSION['suser']->discount);
			}
		}
		//
		$price = $value;
		if( $format == 'number' ) {
			$price = mxx_pdprice_format($price);
			if( $price == '' ) {
				$price = 0;	
			}
			return $prefix . $price;
		} elseif( $format == 'cart' ) {
			$prefix = '';
			if( $price > 0 ) {
				$price = mxx_pdprice_format(abs($price));
			} elseif( $price == 0 || $price == '' ) {
				$price = '0';
			} elseif( $price < 0 ) {
				$prefix = '-';
				$price = mxx_pdprice_format(abs($price));
			}
			return $prefix . '$' . $price;
		} else {
			$prefix = ($price >= 0) ? '' : '- ';
			$price = mxx_pdprice_format(abs($price));
			if( $price == '' ) {
				return '0';
			} else {
				return $prefix . '$' . $price;
			}
		}
		
		return 	$value;
	}
}

function mxx_pdprice_format($value, $precision = 2) {
	if( strpos($value, '.') !== false ) {
		//$value = substr($value, 0, strpos($value, '.') + 1 + $precision + 1);
		//$value = number_format($value, 2, '.', '');
		$value = number_format($value, $precision);
	} else {
		$value = number_format($value, $precision);
	}
	
	return $value;
}
//
?>